package com.lifo.good.service.serviceimpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lifo.good.bean.model.HfGoods;
import com.lifo.good.dao.SearchGoodsMapper;
import com.lifo.good.service.SearchGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * @author why
 * @date 2018/5/2 0002
 */
@Slf4j
@Service
public class SearchGoodsServiceImpl implements SearchGoodsService {

    @Resource
    private SearchGoodsMapper searchGoodsDao;

    @Override
    public PageInfo<HfGoods> searchHfGoodsByPage(int currentPage, int pageSize) {
     com.github.pagehelper.Page page = PageHelper.startPage(currentPage, pageSize);

            List<HfGoods>  rows = searchGoodsDao.searchHfGoodsByPage( );

            //pageHelper无法获取总条数，所以增加手动设置 begin
            PageInfo<HfGoods> pageInfo = new PageInfo<HfGoods>(page.getResult());
            pageInfo.setList(rows);
            //pageHelper无法获取总条数,所以增加手动设置 end
            return pageInfo;
    }


    @Override
    public HfGoods searchHfGoodsByHfUid( Integer hfUid) {
        HfGoods hfGood = null;
        try{
            hfGood =  searchGoodsDao.searchHfGoodsByHfUid(hfUid);
            log.debug("查询到单个商品==>{}",hfGood);
        }catch (Exception e){
            log.error("传入参数==>{},查询商品信息失败==>{}",hfUid,e.getMessage());
            e.printStackTrace();
        }
        return hfGood;
    }

    @Override
    public List<HfGoods> searchNoMatchGoodsByPage(int startIndex, int endIndex) {
        try{
            return searchGoodsDao.searchNoMatchGoodsByPage(startIndex ,endIndex);
        }catch (Exception e){
            log.error("分页查询未匹配商品异常，异常信息为==>{}",e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer searchNoMatchGoodsCount() {
        try{
            return searchGoodsDao.searchNoMatchGoodsCount();
        }catch (Exception e){
            log.error("统计未匹配商品异常，异常信息为==>{}",e.getMessage());
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public void updateHfGoodsCategoryId(HfGoods hfGoods) {
        try{
            searchGoodsDao.updateHfGoodsCategoryId(hfGoods);
        }catch (Exception  e){
            log.error("修改商品类目出错，异常信息为==>{}",e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
    }

    @Override
    public PageInfo<HfGoods> searchHfGoodsByCategory(Long categoryId ,Integer currentPage, Integer pageSize) {


        com.github.pagehelper.Page page = PageHelper.startPage(currentPage, pageSize);

        List<HfGoods>  rows = searchGoodsDao.searchHfGoodsByCategory(categoryId );

        //pageHelper无法获取总条数，所以增加手动设置 begin
        PageInfo<HfGoods> pageInfo = new PageInfo<HfGoods>(page.getResult());
        pageInfo.setList(rows);
        //pageHelper无法获取总条数,所以增加手动设置 end
        return pageInfo;


    }

    @Override
    public List<HfGoods> searchShowcaseGood(int shopId,int randomRow) {
        List<HfGoods>  rows = null;
        try{
            rows = searchGoodsDao.searchRandomHfGoods(shopId,randomRow);
        }catch (Exception e){
            log.error("查询橱窗商品失败==>{}",e.getMessage());
            e.printStackTrace();
        }
        return rows;
    }
}
